/**
 * Created by hyx on 2019/9/12.
 */
function modifyPwd(data) {
    isHomePageLogin = false;
    $("#password").val("");
    var modifyPwdHandler = function() {
        var msgTip = '<p><label>当前密码存在问题：</label>' + data.message + '</p>'; //提示语：设置的密码等级+密码最小长度+密码复杂度
        if (data.levelname) {
            msgTip += '<p>密码修改提示';
            msgTip += '</p><p><label>安全策略等级：</label>' + data.levelname;
        }
        if (data.minlength) {
            msgTip += '</p><p><label>密码最小长度：</label>' + data.minlength;
        }
        if (data.complexity) {
            msgTip += '</p><p><label>密码策略：</label>' + data.complexity + '</p>';
        } else {
            msgTip += '</p>';
        }
        document.getElementById('modifyPwdTip').innerHTML = msgTip;
        InitPop();
        $('#modifyPwd input[name="actionId"]').val(data.actionId);
        $('.inner-container').show();
    }
    // 密码临期修改密码
    if (data.errorCode == '-3') {
        modifyPwdHandler();
    } else {
        // 密码超期
        confirmPwd(data.message, data.errorCode).then(function(res) {
            // 修改密码
            if (res) {
                modifyPwdHandler();
            } else {
                // 取消修改
                yytUtil.msg.stopLoading($(".login-form")[0]);
                $('.btn-img.btn-entry').val('登录');
            }
        })
    }
}
//重置表单
function myPwdFormReset() {
    document.myPwdForm.reset();

    document.myPwdForm.oldPwd.className = "minp";
    document.myPwdForm.newPwd.className = "minp";
    document.myPwdForm.rePwd.className = "minp";

    document.getElementById("dbError").innerHTML = "";
    document.getElementById("errPwd").innerHTML = "";
    document.getElementById("errNewPwd").innerHTML = "";
    document.getElementById("errNewConfirmPwd").innerHTML = "";
    document.getElementById("errVerifycodePwd").innerHTML = "";
    refreshModifyPwdCaptcha();
}

//弹出窗口
function InitPop() {
    myPwdFormReset();

    var ah = $("body").height();
    var aw = $("body").width();
    var wh = $(window).height();
    //alert(bw)
    if (ah < wh) {
        ah = wh;
    }
    $('<div id="fullbg"></div>').appendTo($("body"));
    $("#fullbg").css({
        height: ah,
        width: aw,
        display: "block"
    });
    $(".inner-container").css({
        top: ($(window).height() - $(".inner-container").height()) / 2 - 50 + "px",
        left: ($(window).width() - 650) / 2
    });
}
//关闭弹出窗口
function closePop() {
    yytUtil.msg.stopLoading($(".login-form")[0]);
    $('.btn-img.btn-entry').val('登录');

    $('#fullbg').remove();
    $(".inner-container").hide();
}
//检查旧密码
function checkOldPassword() {
    var oldPwd = document.myPwdForm.oldPwd.value;
    var hasErr = false;
    //检查旧密码
    if (oldPwd.length < 1) {
        document.getElementById("errPwd").innerHTML = "请输入您现在的密码";
        document.getElementById("errPwd").style.display = "inline-block";
        hasErr = true;
    } else {
        document.getElementById("errPwd").style.display = "none";
    }
    return hasErr;
}
//新密码检查
function checkNewPasswordSecurity() {
    //新密码一般检查
    if (!checkNewPassword()) {
        //新密码安全策略检查
        checkLoginPwdSecurtiy("checkNewPwd", true);
    }
}
//检查新密码
function checkNewPassword() {
    var oldPwd = document.myPwdForm.oldPwd.value;
    var newPwd = document.myPwdForm.newPwd.value;
    var errNewPwdMsg = "";
    var hasErr = false;
    if (newPwd.length < 1) {
        errNewPwdMsg = "请输入新密码";
    }
    if (oldPwd == newPwd) {
        document.getElementById("errNewPwd").innerHTML = '新密码和原密码不能相同';
        document.getElementById("errNewPwd").style.display = "inline-block";
        document.myPwdForm.newPwd.className = "minp error";
        hasErr = true;
    } else if (errNewPwdMsg != "") {
        document.getElementById("errNewPwd").innerHTML = errNewPwdMsg;
        document.getElementById("errNewPwd").style.display = "inline-block";
        document.myPwdForm.newPwd.className = "minp error";
        hasErr = true;
    } else {
        //document.getElementById("errNewPwd").style.display = "none";
        document.myPwdForm.newPwd.className = "minp";
    }
    return hasErr;
};

/** 登录密码安全策略校验
 *  action: 校验动作
 *  		checkUserPwd,对用户登录密码进行安全策略校验；
 *  		checkNewPwd，对新密码进行安全策略校验
 *  		saveNewPwd，新密码保存校验
 */
function checkLoginPwdSecurtiy(action, isAsync) {
    var checkFormReturn = false; //校验结果返回值，true：校验通过，false：校验不通过
    // 参数
    var dataParams = {};
    dataParams['action'] = action;
    dataParams['username'] = $("#username").val();
    dataParams['_dc'] = (new Date()).getTime();
    dataParams['actionId'] = $('#modifyPwd input[name="actionId"]').val();
    //点登录，对登录密码进行安全策略校验
    if (action == "checkUserPwd") {
        dataParams['password'] = document.myForm.password.value;
        if (document.myForm.enCodeType) {
            dataParams['enCodeType'] = document.myForm.enCodeType.value;
        }
    } else if (action == "checkNewPwd") { //修改密码弹窗，对新密码进行安全策略校验
        dataParams['newPwd'] = eba(document.myPwdForm.newPwd.value);
    } else if (action == "saveNewPwd") { //修改密码弹窗，对新密码进行保存
        dataParams['oldPwd'] = eba(document.myPwdForm.oldPwd.value);
        dataParams['newPwd'] = eba(document.myPwdForm.newPwd.value);
        dataParams['rePwd'] = eba(document.myPwdForm.rePwd.value);
        dataParams['verifycode'] = document.myPwdForm.verifycode.value;
    }
    //请求后台密码安全策略校验servlet服务
    $.ajax({
        url: "checkPwd",
        type: "GET",
        async: isAsync, //同步请求
        data: dataParams,
        dataType: 'json',
        timeout: 30000,
        error: function(err) {
            if (err && err.responseJSON) {
                yytUtil.msg.showErrorMsg(err.responseJSON.message || "系统内部异常，稍后再试!");
            } else if (err && err.responseText) {
                yytUtil.msg.showErrorMsg(err.responseText || "系统内部异常，稍后再试!");
            } else {
                yytUtil.msg.showErrorMsg(err || "系统内部异常，稍后再试!");
            }
        },
        success: function(data, status) {
            //密码安全策略校验
            if (action == 'checkUserPwd') {
                if (data && data.checkResult == 'false' && data.checkResultMsg) {
                    $("#errorDiv").html('');
                    $("#errorDiv").append('<s></s><span id="errorSpan">' + data.checkResultMsg + '</span>');
                    checkFormReturn = false;
                    $('#vcodeId').click();
                } else {
                    //校验通过
                    checkFormReturn = true;
                }
            }
            //新密码安全策略校验
            if (action == "checkNewPwd") {
                if (data) {
                    // 新密码安全策略校验是否通过
                    if (data.checkResult == 'false') {
                        document.getElementById('errNewPwd').innerHTML = data.checkResultMsg;
                        document.getElementById("errNewPwd").style.display = "";
                        //document.myPwdForm.newPwd.focus();
                        checkFormReturn = false;
                    } else {
                        document.getElementById('errNewPwd').innerHTML = "";
                        document.myPwdForm.rePwd.focus();
                        checkFormReturn = true;
                    }
                } else {
                    checkFormReturn = true;
                }
            }
            //修改密码
            if (action == 'saveNewPwd') {
                if (data.checkResult && data.checkResult == 'false') {
                    //修改密码不成功，返回错误提示
                    // myPwdFormReset();
                    modifyPasswordErrorTip(data);
                    checkFormReturn = false;
                    refreshModifyPwdCaptcha();
                } else {
                    checkFormReturn = true;
                }
            }

        }

    });
    return checkFormReturn;
}
//检查新密码确认
function checkNewPasswordConfirm() {
    var newPwd = document.myPwdForm.newPwd.value;
    var rePwd = document.myPwdForm.rePwd.value;
    var hasErr = false;
    if (rePwd != newPwd) {
        document.getElementById("errNewConfirmPwd").innerHTML = "您两次输入的新密码不一致，请确认";
        document.getElementById("errNewConfirmPwd").style.display = "inline-block";
        document.myPwdForm.rePwd.className = "minp error";
        hasErr = true;
    } else {
        document.getElementById("errNewConfirmPwd").style.display = "none";
        document.myPwdForm.rePwd.className = "minp";
    }
    return hasErr;
}

//修改密码，预提交表单校验
function checkMyPwdForm() {
    //检查旧密码
    if (checkOldPassword()) {
        document.myPwdForm.oldPwd.focus();
        return false;
    }

    //检查新密码
    if (checkNewPassword()) {
        document.myPwdForm.newPwd.focus();
        return false;
    }

    //检查新密码确认
    if (checkNewPasswordConfirm()) {
        document.myPwdForm.rePwd.focus();
        return false;
    }
    // 密码安全策略校验
    if (!checkLoginPwdSecurtiy("saveNewPwd", false)) {
        return false;
    };

    alert("新密码保存成功！");
    closePop();
    location.reload();
    // 修改密码后自动登录
    //document.myForm.psword.value = document.myPwdForm.newPwd.value;
    //if(beforeFromSubmit()){
    // document.myForm.submit();
    //}

    return false;
}

function modifyPasswordErrorTip(data) {
    document.getElementById('dbError').innerHTML = "";
    //旧密码错误
    if (data.errorType == 'oldPwd') {
        document.getElementById('errPwd').innerHTML = data.errorMsg;
        document.getElementById("errPwd").style.display = "";
        document.myPwdForm.oldPwd.focus();
    } else if (data.errorType == 'newPwd') {
        //新密码错误
        document.getElementById('errNewPwd').innerHTML = data.errorMsg;
        document.getElementById("errNewPwd").style.display = "";
        document.myPwdForm.newPwd.focus();
    } else if (data.errorType == 'rePwd') {
        //确认密码错误
        document.getElementById('errNewConfirmPwd').innerHTML = data.errorMsg;
        document.getElementById("errNewConfirmPwd").style.display = "";
        document.myPwdForm.rePwd.focus();
    } else if (data.errorType == 'shutdown') {
        //未找到用户，关闭弹窗
        diag.close();
    } else if (data.errorType == 'verifycode') {
        //验证码错误
        document.getElementById('errVerifycodePwd').innerHTML = data.errorMsg;
        document.getElementById("errVerifycodePwd").style.display = "";
    } else {
        //保存密码错误
        document.getElementById('dbError').innerHTML = data.errorMsg;
    }
}

function refreshModifyPwdCaptcha() {
    $("#modifypwd_captcha_img").attr("src", "/cas/captcha.jpg?type=modifypwd&id=" + Date.parse(new Date()));
}
// 显示/加密 输入的密码
function showPassWord(name) {
    var $input = $('.minp[name="' + name + '"]');
    $input.next().toggleClass('open');
    if ($input.attr('type') === 'password') {
        $input.attr('type', 'text');
    } else {
        $input.attr('type', 'password');
    }
}

// 密码超期、临期提醒弹窗
function confirmPwd(msg, errorCode) {
    var deferred = $.Deferred();

    var html = '';

    html += '<div class="pwd-confirm">';
    html += '<div class="pwd-confirm-inner">';
    html += '<div class="inner-header"></div>';
    html += '<div class="inner-content"><div class="message">' + msg + '</div></div>';
    html += '<div class="inner-footer">';
    html += '<input type="button" class="btn btn-cancel" value="' + (errorCode == '-3' ? '下次修改' : '取消') + '"/>';
    html += '<input type="button" class="btn btn-modify" value="修改密码"/>';
    html += '</div>';
    html += '</div></div>';
    html += '<div class="pwd-confirm-mask"></div>';

    $('body').append(html);

    // 按钮点击事件
    $('.pwd-confirm .btn-cancel').on('click', function() {
        deferred.resolve(false);
        $('.pwd-confirm').remove();
        $('.pwd-confirm-mask').remove();
    });
    $('.pwd-confirm .btn-modify').on('click', function() {
        deferred.resolve(true);
        $('.pwd-confirm').remove();
        $('.pwd-confirm-mask').remove();
    });

    return deferred;
}